/*
 * 算法测试
 * 任何test包以外的类严禁调用此类！
 * 
 */

package test;

import java.text.DateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import model.NewPayRecord;
import control.Parser;
import exception.ViewException;

public class CalculateBalance {

	public CalculateBalance() {

	}

	// 通过Parser读数据库view，将结果存入LinkedHashMap
	//参数cardID:为0则检索所有数据，不为0则查询相应ID的数据
	@SuppressWarnings("deprecation")
	public LinkedHashMap<Date, Double> makeHashMap(String viewName, String cardID,
			LinkedHashMap<Date, Double> hashmap) throws ViewException{

		Double balanceValue = 0.1;

		for (NewPayRecord record : Parser.getViewRecord(viewName, cardID)) {

			Date tradeDate = new Date();
			Date tradeDateBefore = new Date();


			tradeDateBefore.setYear(record.getYear() - 1900);
			tradeDateBefore.setMonth(record.getMonth() - 1);
			tradeDateBefore.setDate(record.getDate() - 1);

			// hashmap.put(tradeDateBefore, balanceValue);


			balanceValue = Arith.round(record.getBalanceValue(), 2);

			tradeDate.setYear(record.getYear() - 1900);
			tradeDate.setMonth(record.getMonth() - 1);
			tradeDate.setDate(record.getDate());

			hashmap.put(tradeDate, balanceValue);

		}
		return hashmap;

	}
	
	//modified by zy@12.12: 新增参数tableName,原来的第一个参数，原来是viewName,改为是"recharge,pay,query,other"中的一个，然后直接用sql进行查询
	@SuppressWarnings("deprecation")
	public LinkedHashMap<Date, Double> makeHashMap(String tableName,String mark, String cardID,
			LinkedHashMap<Date, Double> hashmap) throws ViewException{

		Double balanceValue = 0.1;

		for (NewPayRecord record : Parser.getTableRecord(tableName, mark, cardID)) {

			Date tradeDate = new Date();
			Date tradeDateBefore = new Date();


			tradeDateBefore.setYear(record.getYear() - 1900);
			tradeDateBefore.setMonth(record.getMonth() - 1);
			tradeDateBefore.setDate(record.getDate() - 1);
			tradeDateBefore.setHours(0);
			tradeDateBefore.setMinutes(0);
			tradeDateBefore.setSeconds(0);

			// hashmap.put(tradeDateBefore, balanceValue);


			balanceValue = Arith.round(record.getBalanceValue(), 2);

			tradeDate.setYear(record.getYear() - 1900);
			tradeDate.setMonth(record.getMonth() - 1);
			tradeDate.setDate(record.getDate());
			tradeDate.setHours(0);
			tradeDate.setMinutes(0);
			tradeDate.setSeconds(0);

			hashmap.put(tradeDate, balanceValue);

		}
		return hashmap;

	}
	

	// 实现两个HashMap的合并，返回(map_a*weight_a+map_b*weight_b)
	// 当map_a为充值记录是，weight_a为1；当map_a为余额时，weight_a为0，防止重复累加
	public LinkedHashMap<Date, Double> mergeHashMaps(
			LinkedHashMap<Date, Double> map_a,
			LinkedHashMap<Date, Double> map_b, int weight_a, int weight_b) {

		LinkedHashMap<Date, Double> resultMap = new LinkedHashMap<Date, Double>();

		int size_a = map_a.keySet().size();
		int size_b = map_b.keySet().size();
		if(size_a == 0)
			return map_b;
		else if(size_b == 0) 
			return map_a;
		
		Iterator<Date> a = map_a.keySet().iterator();
		Iterator<Date> b = map_b.keySet().iterator();
		double sum = 0.0;
		Date date_a;
		Date date_b;
		date_a = (Date) a.next();
		date_b = (Date) b.next();
		// Date date_aa = (Date) a.next();

		while (a.hasNext()) {

			if (date_b.getTime()-date_a.getTime() < -1000) {

				while (date_b.before(date_a)) {

					sum = Arith.add(sum, Arith.round(map_b.get(date_b), 2)
							* weight_b);
					resultMap.put(date_b, sum);


					date_b = (Date) b.next();

				}

			} else if (date_b.getTime()-date_a.getTime() <= 1000) {

				sum = Arith.add(
						sum,
						Arith.add(Arith.round(map_a.get(date_a), 2),
								Arith.round(map_b.get(date_b), 2) * weight_b));
				resultMap.put(date_a, sum);

				date_a = (Date) a.next();
				date_b = (Date) b.next();

			} else if (date_b.getTime()-date_a.getTime() > 1000) {

				sum = Arith.add(sum * weight_a,
						Arith.round(map_a.get(date_a), 2));
				resultMap.put(date_a, sum);

				date_a = (Date) a.next();

			}

		}

		while (b.hasNext()) {

			sum = Arith.add(sum, Arith.round(map_b.get(date_b), 2) * weight_b);
			resultMap.put(date_b, sum);
			date_b = (Date) b.next();

		}

		return resultMap;
	}

	// todo:插值
	@SuppressWarnings("deprecation")
	public LinkedHashMap<Date, Double> makeInsertedHashMap(
			LinkedHashMap<Date, Double> hashmap) {

		LinkedHashMap<Date, Double> resultHashMap = new LinkedHashMap<Date, Double>();

		Iterator<Date> a = hashmap.keySet().iterator();
		Date date_a;
		Date date_b;
		date_a = (Date) a.next();
		date_b = date_a;
		// Date date_aa = (Date) a.next();

		resultHashMap.put(date_a, hashmap.get(date_a));

		while (a.hasNext()) {

			date_b = (Date) a.next();

			String s = DateFormat.getDateInstance().format(date_b);

			if (getYesterDay(date_b).equals(date_a)) {

				resultHashMap.put(date_b, hashmap.get(date_b));

				date_a = date_b;

			} else if (getYesterDay(date_b).after(date_a)) {

				resultHashMap.put(getYesterDay(date_b),
						resultHashMap.get(date_a));

				resultHashMap.put(date_b, hashmap.get(date_b));

				date_a = date_b;
				
		//		date_b = getYesterDay(date_b);

			} else if (date_b.equals(date_a)) {

				 date_b = (Date) a.next();

			}

		}

		for (Date date : resultHashMap.keySet()) {
			String s = DateFormat.getDateInstance().format(date);
		}

		return resultHashMap;

	}

	@SuppressWarnings("deprecation")
	public Date getYesterDay(Date date) {

		Date yesterday = new Date();
		// Calendar tempDay;
		//
		// tempDay.set(date.getYear(), date.getMonth(), date.getDate());

		if (date.getMonth() == 1 && date.getDay() == 1) {
			yesterday.setYear(date.getYear() - 1);
			yesterday.setMonth(12);
			yesterday.setDate(31);
		} else if (date.getDay() == 1) {
			yesterday.setYear(date.getYear());
			yesterday.setMonth(date.getMonth() - 1);
			yesterday.setDate(31);
		} else {
			yesterday.setYear(date.getYear());
			yesterday.setMonth(date.getMonth());
			yesterday.setDate(date.getDate() - 1);
		}

		return yesterday;
	}

//	public static void main(String args[]) {
//
//		String merge_onlinepay = "merge_onlinepay";
//
//		String merge_recharge = "merge_recharge";
//
//		int weight_a = 1;
//		int weight_b = -1;
//
//		LinkedHashMap<Date, Double> onlinepayMap = new LinkedHashMap<Date, Double>();
//
//		LinkedHashMap<Date, Double> rechargeMap = new LinkedHashMap<Date, Double>();
//
//		LinkedHashMap<Date, Double> resultMap = new LinkedHashMap<Date, Double>();
//
//		CalculateBalance calBalance = new CalculateBalance();
//
//		onlinepayMap = calBalance.makeHashMap(merge_onlinepay, onlinepayMap);
//
//		rechargeMap = calBalance.makeHashMap(merge_recharge, rechargeMap);
//
//		resultMap = calBalance.mergeHashMaps(rechargeMap, onlinepayMap,
//				weight_a, weight_b);
//
//		for (Date date : resultMap.keySet()) {
//			String s = DateFormat.getDateInstance().format(date);
//		}
//
//		calBalance.makeInsertedHashMap(resultMap);
//	}

}
